package com.bdth.mixapi.api.controller.traffic;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bdth.mixapi.api.entity.Device;
import com.bdth.mixapi.api.entity.Onlinerate;
import com.bdth.mixapi.api.entity.RdOnlinerate;
import com.bdth.mixapi.api.service.DeviceService;
import com.bdth.mixapi.api.service.OnlinerateService;
import com.bdth.mixapi.api.vo.CommonList;
import com.bdth.mixapi.api.vo.Dvo;
import com.bdth.mixapi.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/5/8.
 */
@Api("车辆位置信息")
@RestController
@RequestMapping("/traffic")
public class DeviceTypeController {
    @Autowired
    DeviceService deviceService;

    @Autowired
    OnlinerateService onlinerateService;

    @ApiOperation(value="设备位置列表", notes="")
    @RequestMapping(value = "device/list", method = RequestMethod.GET)
    public CommonList<Dvo> list(){
        //查询有数据的最后一天
        QueryWrapper<Device> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("uptime");
        wrapper.eq("type",2);
        wrapper.last("limit 1");
        List<Device> list = deviceService.list(wrapper);
        List<Dvo> l = new ArrayList<>();

        if(list!=null && list.size()>0) {
            QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("uptime", DateUtil.format(list.get(0).getUptime()));
            queryWrapper.eq("type",2);
            List<Device> deviceList = deviceService.list(queryWrapper);
            for(int i=0;i<deviceList.size();i++){
                Dvo d = new Dvo();
                d.setP(deviceList.get(i).getType());
                d.setT(deviceList.get(i).getLat());
                d.setG(deviceList.get(i).getLng());
                l.add(d);
            }
            return CommonList.success(l);
        }else {
            return CommonList.fail("没有数据");
        }
    }

    @ApiOperation(value="最新加入单位", notes="")
    @RequestMapping(value = "new/depart", method = RequestMethod.GET)
    public CommonList<Onlinerate> getDepart(){
        QueryWrapper<Onlinerate> wrapper = new QueryWrapper<>();
        wrapper.eq("StatisticDate","20190507");
        wrapper.orderByDesc("DepId");
        wrapper.last("limit 12");
        List<Onlinerate> list = onlinerateService.list(wrapper);
        return CommonList.success(list);
    }
}
